package main.java.controller;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import main.java.DAO.IUserInfoBasicDAO;
import main.java.constant.StateEnum;
import main.java.model.GuardianModel;
import main.java.model.UserInfoBasicModel;
import main.java.service.ResourceService;
import main.java.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController  {
	@Autowired
	@Qualifier("userService")
	private UserService userService;
	@Autowired
	@Qualifier("IUserInfoBasicDAO")
	private IUserInfoBasicDAO userInfoBasicDAO;

	/*** registry ***/
	@RequestMapping(method = RequestMethod.GET, value = "/registry/")
	public String registry(ModelMap modelMap) {
		UserInfoBasicModel userInfoBasicModel = new UserInfoBasicModel();
		modelMap.addAttribute("userInfoBasicModel", userInfoBasicModel);
		
		return "forward:/user/registry.jsp";
	}

	@RequestMapping(method = RequestMethod.POST, value = "/registry/")
	public String registry(HttpServletRequest request,
			ModelMap modelMap,
			UserInfoBasicModel userInfoBasicModel,
			BindingResult result)	 {
		
		String correctKaptcha = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		//check form kaptcha
		if(request.getParameter("kaptcha").equals(correctKaptcha) == false)	{
			modelMap.put("state", StateEnum.FORM_KAPTCHA_INCORRECT);
		}
		else	{
			//registry
			StateEnum state = StateEnum.OTHER;
			state = userService.registry(userInfoBasicModel);
			modelMap.put("state", state);
		}
		
		return "main/resources/JSP/state";
	}

	/*** update ***/
	@RequestMapping(method = RequestMethod.GET, value = "/update/")
	public String update(ModelMap modelMap) 	{
		modelMap.addAttribute("userUpdateFormTransmitter", userService.assembleUserUpdateFormTransmitter(null));
		
		return "forward:/user/update.jsp";
	}
	
	@RequestMapping(method = RequestMethod.POST,  value = "/update/")
	public String update(HttpServletRequest request,
			ModelMap modelMap,
			UserUpdateFormTransmitter userUpdateFormTransmitter,
			BindingResult result) 	{		
		//update
		StateEnum state = StateEnum.OTHER;
		state = userService.update(userUpdateFormTransmitter);
		modelMap.put("state", state);
		
		//����springsecuritycontext�е�MyUserDetails�е�UserInfoBasicModel����
		userService.updateCurrentLoginUserInfoBasicModel();

		return "main/resources/JSP/state";
	}
	
	/*** listView ***/
	@RequestMapping(method = RequestMethod.GET, value = "/listView/")
	public String listView(ModelMap modelMap) 	{
		modelMap.addAttribute("userUpdateFormTransmitters", userService.assembleAllUserUpdateFormTransmitters());

		return "forward:/user/listView.jsp";
	}
	
	//----- Form Transmitter -----
	public static class UserUpdateFormTransmitter		{
		@Valid
		private UserInfoBasicModel userInfoBasicModel;
		@Valid
		private List<GuardianModel> guardianModels;
		private String old_password;					//������ (���������, �����Ϊempty��null)
		private String new_password;					//������
		
		@Override
		public String toString() {
			return "UserUpdateFormTransmitter [userInfoBasicModel="
					+ userInfoBasicModel + ", guardianModels=" + guardianModels
					+ ", old_password=" + old_password + ", new_password="
					+ new_password + "]";
		}
		
		public UserInfoBasicModel getUserInfoBasicModel() {
			return userInfoBasicModel;
		}
		public void setUserInfoBasicModel(UserInfoBasicModel userInfoBasicModel) {
			this.userInfoBasicModel = userInfoBasicModel;
		}
		public List<GuardianModel> getGuardianModels() {
			return guardianModels;
		}
		public void setGuardianModels(List<GuardianModel> guardianModels) {
			this.guardianModels = guardianModels;
		}
		public String getOld_password() {
			return old_password;
		}
		public void setOld_password(String old_password) {
			this.old_password = old_password;
		}
		public String getNew_password() {
			return new_password;
		}
		public void setNew_password(String new_password) {
			this.new_password = new_password;
		}
	}
}
